package org.example.netty.test.nio.netty.chat.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.example.netty.test.nio.netty.chat.bean.OutGroupMessage;
import org.example.netty.test.nio.netty.chat.bean.OutUserMessage;
import org.example.netty.test.nio.netty.chat.bean.SystemMessage;
import org.example.netty.test.nio.netty.chat.common.SystemMessageType;
import org.example.netty.test.nio.netty.chat.session.GlobalContent;

/**
 * @author zp
 * @version 1.0
 * @description: 处理消息OutGroupMessage
 * @date 2023/4/6 18:29
 */
@Slf4j
public class OutGroupMessageHandler extends SimpleChannelInboundHandler<OutGroupMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, OutGroupMessage msg) throws Exception {
        log.debug("退出群聊的消息：{}",msg);
        GlobalContent globalContent = GlobalContent.getInstance();
        String groupChatName = msg.getGroupChatName();
        // 校验退出的群是否存在
        if (globalContent.isExistGroupName(groupChatName)) {
            globalContent.removeGroupChat(groupChatName,msg.getSender());
            ctx.channel().writeAndFlush(msg);
        }else {
            SystemMessage.sendFailMessage(SystemMessageType.OUT_GROUP_FAIL,ctx);
        }


    }
}
